package com.party.core.service.member.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.member.MemberAddressReadDao;
import com.party.core.dao.write.member.MemberAddressWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.city.Area;
import com.party.core.model.member.MemberAddress;
import com.party.core.service.city.IAreaService;
import com.party.core.service.member.IMemberAddressService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 商城用户地址库
 * @Date:Created in 2017/9/12 0012 下午 17:11.
 */
@Service
public class MemberAddressService implements IMemberAddressService {
    @Autowired
    private MemberAddressReadDao memberAddressReadDao;
    @Autowired
    private MemberAddressWriteDao memberAddressWriteDao;
    @Autowired
    private IAreaService areaService;


    /**
     * 插入用户地址信息
     *
     * @param memberAddress 用户地址信息
     * @return 插入结果（true/false）
     */
    public String insert(MemberAddress memberAddress) {
        BaseModel.preInsert(memberAddress);
        boolean result = memberAddressWriteDao.insert(memberAddress);
        if (result) {
            return memberAddress.getId();
        }
        return null;
    }

    /**
     * 更新用户地址信息
     *
     * @param memberAddress 用户地址信息
     * @return 更新结果（true/false）
     */
    public boolean update(MemberAddress memberAddress) {
        memberAddress.setUpdateDate(new Date());
        return memberAddressWriteDao.update(memberAddress);
    }

    /**
     * 逻辑删除用户地址信息
     *
     * @param id 会员主键
     * @return 删除结果（true/false）
     */
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberAddressWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除用户地址信息
     *
     * @param id 会员主键
     * @return 删除结果（true/false）
     */
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberAddressWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除用户地址信息
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberAddressWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除用户地址信息
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberAddressWriteDao.batchDelete(ids);
    }

    /**
     * 根据主键获取用户地址信息
     *
     * @param id 主键
     * @return 用户地址信息
     */
    public MemberAddress get(String id) {
        return memberAddressReadDao.get(id);
    }

    /**
     * 分页查询会员列表
     *
     * @param memberAddress 用户地址信息
     * @param page          分页信息
     * @return 会员列表
     */
    public List<MemberAddress> listPage(MemberAddress memberAddress, Page page) {
        return memberAddressReadDao.listPage(memberAddress, page);
    }

    /**
     * 查询所有用户地址信息
     *
     * @param memberAddress 用户地址信息
     * @return 会员列表
     */
    public List<MemberAddress> list(MemberAddress memberAddress) {
        return memberAddressReadDao.listPage(memberAddress, null);
    }

    /**
     * 分页查询用户地址信息
     *
     * @param ids           主键集合
     * @param memberAddress 用户地址信息
     * @param page          分页信息
     * @return 会员列表
     */
    public List<MemberAddress> batchList(@NotNull Set<String> ids, MemberAddress memberAddress, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return memberAddressReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public MemberAddress getDefaultAddress(String memberId) {
        MemberAddress memberAddress = new MemberAddress(memberId);
        memberAddress.setDefaultAddress(MemberAddress.IS_DEFAULT_ADDRESS);
        MemberAddress defaultAddress = memberAddressReadDao.getUnique(memberAddress);
        if (defaultAddress == null) {
            List<MemberAddress> addresses = memberAddressReadDao.listPage(new MemberAddress(memberId), null);
            if (addresses.size() == 0) {
                return null;
            } else {
                addresses.get(0);
            }
        }
        return defaultAddress;
    }

    @Override
    public void cancelDefaultAddress(String memberId) {
        memberAddressWriteDao.cancelDefaultAddress(memberId);
    }

    @Override
    public MemberAddress getBestNew(String memberId) {
        return memberAddressReadDao.getBestNew(memberId);
    }
}
